import matplotlib.pyplot as plt
import numpy as np

# 设置中文字体
plt.rcParams['font.sans-serif'] = ['SimHei', 'DejaVu Sans']
plt.rcParams['axes.unicode_minus'] = False

# 参数设置
initial_distance = 100  # 乌龟领先100米
achilles_speed = 10     # 阿基里斯速度10m/s
turtle_speed = 1        # 乌龟速度1m/s

# 理论追上时间
theoretical_time = initial_distance / (achilles_speed - turtle_speed)

# 模拟追赶过程
positions_achilles = [0]
positions_turtle = [initial_distance]
times = [0]
distances = [initial_distance]  # 两者之间的距离
time_elapsed = 0

# 模拟前几次追赶
for i in range(10):
    # 计算阿基里斯到达乌龟当前位置所需时间
    current_distance = positions_turtle[-1] - positions_achilles[-1]
    time_to_catch = current_distance / achilles_speed
    time_elapsed += time_to_catch
    
    # 更新位置
    new_achilles_pos = positions_turtle[-1]
    new_turtle_pos = positions_turtle[-1] + time_to_catch * turtle_speed
    
    positions_achilles.append(new_achilles_pos)
    positions_turtle.append(new_turtle_pos)
    times.append(time_elapsed)
    distances.append(new_turtle_pos - new_achilles_pos)

# 创建图形
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 6))

# 第一个子图：位置随时间变化
ax1.plot(times, positions_achilles, 'ro-', linewidth=2, markersize=6, label='阿基里斯')
ax1.plot(times, positions_turtle, 'bo-', linewidth=2, markersize=6, label='乌龟')
ax1.axhline(y=theoretical_time*(achilles_speed-turtle_speed), color='g', linestyle='--', alpha=0.7, label='追上位置')

# 标记追上点
ax1.plot(theoretical_time, theoretical_time*(achilles_speed-turtle_speed), 'g*', markersize=15, label='追上点')

ax1.set_xlabel('时间 (秒)', fontsize=12)
ax1.set_ylabel('位置 (米)', fontsize=12)
ax1.set_title('阿基里斯追乌龟 - 位置随时间变化', fontsize=14)
ax1.legend(fontsize=10)
ax1.grid(True, alpha=0.3)

# 设置x轴和y轴范围，使图形更清晰
ax1.set_xlim(0, theoretical_time + 1)
ax1.set_ylim(0, theoretical_time*(achilles_speed-turtle_speed) + 10)

# 第二个子图：剩余距离随追赶次数的变化
ax2.plot(range(len(distances)), distances, 'g^-', linewidth=2, markersize=8)
ax2.set_xlabel('追赶次数', fontsize=12)
ax2.set_ylabel('两者距离 (米)', fontsize=12)
ax2.set_title('每次追赶时的剩余距离', fontsize=14)
ax2.grid(True, alpha=0.3)

# 添加距离标签
for i, d in enumerate(distances):
    if i % 2 == 0 or i == len(distances)-1:  # 避免标签重叠
        ax2.annotate(f'{d:.2f}m', (i, d), textcoords="offset points", 
                    xytext=(0,10), ha='center', fontsize=9)

# 添加理论分析文本框
textstr = f'理论分析:\n初始距离: {initial_distance}m\n阿基里斯速度: {achilles_speed}m/s\n乌龟速度: {turtle_speed}m/s\n追上时间: {theoretical_time:.2f}s\n追上位置: {theoretical_time*(achilles_speed-turtle_speed):.2f}m'
props = dict(boxstyle='round', facecolor='wheat', alpha=0.8)
ax1.text(0.02, 0.98, textstr, transform=ax1.transAxes, fontsize=10,
        verticalalignment='top', bbox=props)

# 添加芝诺悖论解释
paradox_text = "芝诺悖论解析:\n• 看似无限次追赶\n• 但每次时间越来越短\n• 总时间收敛于有限值\n• 实际可以追上"
ax2.text(0.02, 0.98, paradox_text, transform=ax2.transAxes, fontsize=10,
        verticalalignment='top', bbox=dict(boxstyle='round', facecolor='lightblue', alpha=0.8))

plt.tight_layout()
plt.show()

# 输出详细数据
print("=== 阿基里斯追乌龟问题详细分析 ===")
print(f"初始条件: 乌龟领先 {initial_distance}米")
print(f"速度: 阿基里斯 {achilles_speed}m/s, 乌龟 {turtle_speed}m/s")
print(f"理论追上时间: {theoretical_time:.6f}秒")
print(f"理论追上位置: {theoretical_time*(achilles_speed-turtle_speed):.6f}米")
print("\n追赶过程详情:")
print("次数 | 时间(秒) | 阿基里斯位置(米) | 乌龟位置(米) | 剩余距离(米)")
print("-" * 70)
for i in range(min(10, len(times))):
    print(f"{i:2d}   | {times[i]:7.4f} | {positions_achilles[i]:15.4f} | {positions_turtle[i]:13.4f} | {distances[i]:13.4f}")

print(f"\n无限次追赶的时间序列: 10 + 1 + 0.1 + 0.01 + ... = {theoretical_time:.6f}秒")
print("这是一个收敛的等比数列，总和有限!")